package com.tangcan.blog.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.tangcan.blog.Entity.BlogContent;
import com.tangcan.blog.Mapper.BlogContentMapper;
import com.tangcan.blog.Utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@EnableAutoConfiguration
public class BlogService {
    @Autowired
    BlogContentMapper blogContentMapper;

    TokenUtils tokenUtils;
    public boolean isAdmin(String token)
    {
        Map tokenInfo = tokenUtils.getTokenInfo(token);
        String authority = tokenInfo.get("authority").toString().replace("\"","");
        if (authority.equals("admin"))
        {
            return true;
        }
        else {
            return false;
        }
    }

    public JSONObject GetAll(){
        JSONObject resJson = new JSONObject();
        List<BlogContent> BlogList = blogContentMapper.GetAll();
        if (!BlogList.isEmpty())
        {
            resJson.put("code","ok");
            resJson.put("blogList",BlogList);
        }
        else {
            resJson.put("code","err");
            resJson.put("info","Null err");

        }
        return resJson;
    }
    public JSONObject GetAllSub(){
        JSONObject resJson = new JSONObject();
        List<BlogContent> BlogList = blogContentMapper.GetAll();

        for (BlogContent blog : BlogList)
        {
            blog.setContent("");
        }
        if (!BlogList.isEmpty())
        {
            resJson.put("code","ok");
            resJson.put("blogList",BlogList);
        }
        else {
            resJson.put("code","err");
            resJson.put("info","Null err");

        }
        return resJson;
    }



    public JSONObject getPage(String data)
    {
        JSONObject resJson = new JSONObject();
        JSONObject json = JSON.parseObject(data);
        int page = 1;
        String tag = "all";
        if(json.getInteger("page") != null && !json.getString("tag").isEmpty())
        {
            page = json.getInteger("page");
            tag = json.getString("tag");
        }

        int num1  =(page - 1)*5;
        int num2 = page * 5 ;
        List<BlogContent> BlogList;
        System.out.println(tag+num1+num2);
        if (tag.equals("all"))
        {

            BlogList= blogContentMapper.GetPage(num1,num2);
        }
        else {
             BlogList = blogContentMapper.GetPageByTag(tag,num1,num2);

        }
        for(int i=0;i<BlogList.size();i++)
        {
            BlogContent temp = BlogList.get(i);
            //去除markdown格式

            String tmpStr = temp.getContent().replace("#","").replace("`","").replace("[","").replace("]","");
            if (tmpStr.length() > 400)
            {
                tmpStr = tmpStr.substring(0,400);
                temp.setContent(tmpStr);
            }
            temp.setContent(tmpStr);
            BlogList.set(i, temp);
        }

        if (!BlogList.isEmpty())
        {
            resJson.put("code","ok");
            resJson.put("blogList",BlogList);
        }
        else {
            resJson.put("code","err");
            resJson.put("info","Null err");

        }
        return resJson;
    }
    public JSONObject getBlogById(String data)
    {
        JSONObject resJson = new JSONObject();
        JSONObject json = JSON.parseObject(data);
        int id = json.getInteger("id");
        BlogContent blog = blogContentMapper.GetBlog(id);
        if (blog != null)
        {
            resJson.put("code","ok");
            resJson.put("blog",blog);
        }else {
            resJson.put("code","err");
            resJson.put("info","Null err");

        }
        return resJson;
    }
    public JSONObject getPageNum(String data){
        JSONObject json = JSON.parseObject(data);
        JSONObject resJson = new JSONObject();
        String Tag = "all";
        if (!json.getString("tag").isEmpty())
        {
         Tag = json.getString("tag");
        }
        if (Tag.equals("all"))
        {
            int pageNum = blogContentMapper.GetCount();
            resJson.put("code","ok");
            resJson.put("blog",pageNum);
            return resJson;
        }
        else
        {
            int pageNum = blogContentMapper.GetCountByTag(Tag);
            resJson.put("code","ok");
            resJson.put("blog",pageNum);
            return resJson;
        }


    }
    public JSONObject AddBlog(String data)
    {
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {
            Map info = TokenUtils.getTokenInfo(token);
            int id = Integer.parseInt(info.get("id").toString().replace("\"","")) ;
            if (!json.getString("blog").isEmpty())
            {
                BlogContent REQblog = JSONObject.parseObject(json.getString("blog"),BlogContent.class);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date = df.format(new Date());
                REQblog.setCreateTime(date);
                REQblog.setUpdateTime(date);
                REQblog.setUserId(id);
                blogContentMapper.InserBlog(REQblog);
                resJson.put("code","ok");
                resJson.put("info","ok");
                return resJson;
            }
            else {
                resJson.put("code","err");
                resJson.put("info","blog为空");
                return resJson;
            }

        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }

    }

    public JSONObject UpdateBlog(String data)
    {
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {
            if (!json.getString("blog").isEmpty())
            {
                BlogContent REQblog = JSONObject.parseObject(json.getString("blog"),BlogContent.class);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date = df.format(new Date());
                REQblog.setUpdateTime(date);
                blogContentMapper.UpdateBlogById(REQblog);
                resJson.put("code","ok");
                resJson.put("info","ok");
                return resJson;
            }
            else {
                resJson.put("code","err");
                resJson.put("info","blog为空");
                return resJson;
            }

        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }
    }

    public JSONObject RemoveBlog(String data)
    {
        JSONObject json = JSON.parseObject(data, Feature.OrderedField);
        JSONObject resJson = new JSONObject();
        String token = json.getString("token");
        if (tokenUtils.verify(token) && isAdmin(token))
        {

                int id  = json.getInteger("id");
                int efc =  blogContentMapper.RemoveBlog(id);
                if (efc > 0 )
                {
                    resJson.put("code","ok");
                    resJson.put("info","ok");
                    return resJson;
                }
                else {
                    resJson.put("code","err");
                    resJson.put("info","删除失败");
                    return resJson;
                }
        }
        else
        {
            resJson.put("code","err");
            resJson.put("info","鉴权失败");
            return resJson;
        }
    }
    public JSONObject GetAllTag(){
        JSONObject resJson = new JSONObject();
        resJson.put("code","ok");
        resJson.put("tagList",blogContentMapper.GetAllTag());

        return resJson;
}
public JSONObject SearchBlog(String data)
{
    JSONObject json = JSON.parseObject(data);
    JSONObject resJson = new JSONObject();

    String keyWord = json.getString("keyWord");

    List<BlogContent> blogList =  blogContentMapper.SearchBlog(keyWord);
    if (!blogList.isEmpty())
    {
        resJson.put("code","ok");
        resJson.put("blogList",blogList);

    }else
    {
        resJson.put("code","err");
        resJson.put("info","null");
    }
    return resJson;


}
}
